<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use mbilling\BillingBundle\Model\UsersModel;

class PanelController extends Controller
{
    public function indexAction()
    {
        /*$securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();
        if ($userCurrent == 'anon.')
        {
            return new RedirectResponse($this->generateUrl('login'));
        }*/
        return $this->render('BillingBundle:Panel:user_index.html.twig', array());
    }
    public function autologinAction()
    {
        $hash = $_POST['hash'];
        $em = $this->get('doctrine.orm.entity_manager');
        $user_model = new UsersModel($em);
        $users = $user_model->findAll();
        foreach($users as $user)
        {
            $user_hash = $user->getAutologinHash();
            if ($hash == $user_hash)
            {
                //$users_provider = $this->container->get('users_provider');
                //$user_for_token = $users_provider->refreshUser($user);
                $securityContext = $this->container->get('security.context');
                $token = new UsernamePasswordToken($user, null, 'users_provider', $user->getRoles());
                $securityContext->setToken($token);
                return new Response('true');
            }
        }
        return new Response('false');
    }
    public function checkloginAction()
    {
        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();
        if ($userCurrent == 'anon.')
        {
            return new Response('false');
        }
        return new Response('true');
    }
}
